﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using IP.Core.IPData;
using IP.Core.IPUserService;
using BKIDS.CDBNames;
using IP.Core.IPCommon;
using BKIDS;
using BKIUS;
using System.Data;


public partial class BaoCao_F643_Bao_cao_danh_sach_cong_viec_qua_han : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            m_txt_ngay_xem.SelectedDate = DateTime.Today.Date;
            load_cbo_du_an();            
            load_data_2_griv();
        }
    }
    #region Members
    US_V_DM_THANH_VIEN_CONG_VIEC_DU_AN m_us_v_dm_thanh_vien_da = new US_V_DM_THANH_VIEN_CONG_VIEC_DU_AN();
    DS_V_DM_THANH_VIEN_CONG_VIEC_DU_AN m_ds_v_dm_thanh_vien_da = new DS_V_DM_THANH_VIEN_CONG_VIEC_DU_AN();
    #endregion

    #region Public Interfaces

    #endregion

    #region Private Methods    
    private void load_cbo_du_an()
    {
        US_DM_DU_AN v_us_dm_du_an = new US_DM_DU_AN();
        DS_DM_DU_AN v_ds_dm_du_an = new DS_DM_DU_AN();
        v_us_dm_du_an.FillDataset(v_ds_dm_du_an);

        m_cbo_du_an.DataTextField = DM_DU_AN.TEN_DU_AN;
        m_cbo_du_an.DataValueField = DM_DU_AN.ID;

        m_cbo_du_an.DataSource = v_ds_dm_du_an.DM_DU_AN;
        m_cbo_du_an.DataBind();
    }
    private void load_data_2_griv()
    {
        if (!m_cbo_du_an.SelectedValue.Trim().Equals("") && m_txt_ngay_xem.PostedDate.Trim() != "")
        {
            m_us_v_dm_thanh_vien_da.fill_dataset_cong_viec_qua_han_by_du_an(m_ds_v_dm_thanh_vien_da, CIPConvert.ToDecimal(m_cbo_du_an.SelectedValue), m_txt_ngay_xem.SelectedDate);
            m_grv_thanh_vien.DataSource = m_ds_v_dm_thanh_vien_da.V_DM_THANH_VIEN_CONG_VIEC_DU_AN;
            m_grv_thanh_vien.DataBind();
            if (m_ds_v_dm_thanh_vien_da.V_DM_THANH_VIEN_CONG_VIEC_DU_AN == null || m_ds_v_dm_thanh_vien_da.V_DM_THANH_VIEN_CONG_VIEC_DU_AN.Count == 0)
                m_lbl_thong_bao.Text = "Không có dữ liệu";
            else m_lbl_thong_bao.Text = "";
        }
    }
    private void load_data_2_excel_search()
    {
        if (!m_cbo_du_an.SelectedValue.Trim().Equals("") && m_txt_ngay_xem.PostedDate.Trim() != "")
        {
            m_ds_v_dm_thanh_vien_da.Clear();
            m_us_v_dm_thanh_vien_da.fill_dataset_cong_viec_qua_han_by_du_an(m_ds_v_dm_thanh_vien_da, CIPConvert.ToDecimal(m_cbo_du_an.SelectedValue), m_txt_ngay_xem.SelectedDate);
        }
    }
    #endregion

    #region Export Excel
    private void loadDSExprort(ref string strTable)
    {
        int v_i_so_thu_tu = 0;
        // Mỗi cột dữ liệu ứng với từng dòng là label
        foreach (DataRow grv in this.m_ds_v_dm_thanh_vien_da.V_DM_THANH_VIEN_CONG_VIEC_DU_AN.Rows)
        {
            strTable += "\n<tr>";
            strTable += "\n<td style='width:12%;' class='cssTitleReport' nowrap='nowrap'>" + ++v_i_so_thu_tu + "</td>";
            strTable += "\n<td style='width:12%;' class='cssTitleReport' nowrap='nowrap'>" + CIPConvert.ToStr(grv[V_DM_THANH_VIEN_CONG_VIEC_DU_AN.TEN_CONG_VIEC]) + "</td>";
            strTable += "\n<td style='width:12%;' class='cssTitleReport' nowrap='nowrap'>" + CIPConvert.ToStr(grv[V_DM_THANH_VIEN_CONG_VIEC_DU_AN.THOI_GIAN_KET_THUC], "dd/MM/yyyy") + "</td>";           
            strTable += "\n<td style='width:12%;' class='cssTitleReport' nowrap='nowrap'>" + CIPConvert.ToStr(grv[V_DM_THANH_VIEN_CONG_VIEC_DU_AN.TEN_DAY_DU]) + "</td>";
            strTable += "\n<td style='width:12%;' class='cssTitleReport' nowrap='nowrap'>" + CIPConvert.ToStr(grv[V_DM_THANH_VIEN_CONG_VIEC_DU_AN.TRANG_THAI_CONG_VIEC], "p") + "</td>";

            strTable += "\n</tr>";
        }
    }

    private void loadTieuDe(ref string strTable)
    {
        load_data_2_excel_search();
        strTable += "<table cellpadding='2' cellspacing='0' class='cssTableReport'>";
        strTable += "\n<tr>";
        strTable += "\n<td><align='center' class='cssTableView' style='width:100%;' nowrap='nowrap'>  </td>";
        strTable += "\n<td><align='center' class='cssTableView' style='width:100%;' nowrap='nowrap'>  </td>";
        strTable += "\n<td><align='center' class='cssTableView' style='width: 100%;  height: 40px; font-size: large; color:White; background-color:#810C15;' nowrap='wrap'>BKI_PRO - DANH SÁCH CÔNG VIỆC ĐÃ QUÁ HẠN" + "</td>";
        strTable += "\n</tr>";
        //
        strTable += "\n<tr>";
        strTable += "\n<td><align='center' class='cssTableView' style='width:100%;' nowrap='nowrap'>  </td>";
        strTable += "\n<td><align='center' class='cssTableView' style='width:100%;' nowrap='nowrap'>  </td>";
        strTable += "\n<td><align='center' class='cssTableView' style='width: 100%;  height: 40px; font-size: large; color:White; background-color:#810C15;' nowrap='wrap'> " + m_cbo_du_an.SelectedItem +"</td>";
        strTable += "\n</tr>";
        //
        strTable += "\n<tr>";
        strTable += "\n<td><align='center' class='cssTableView' style='width:100%;' nowrap='nowrap'>  </td>";
        strTable += "\n<td><align='center' class='cssTableView' style='width:100%;' nowrap='nowrap'>  </td>";
        strTable += "\n<td><align='center' class='cssTableView' style='width: 100%;  height: 40px; font-size: large; color:White; background-color:#810C15;' nowrap='wrap'> Đến ngày: " + m_txt_ngay_xem.PostedDate + "</td>";
        strTable += "\n</tr>";
        //
        strTable += "\n<tr>";
        strTable += "\n<td><align='center' class='cssTableView' style='width:100%;' nowrap='nowrap'>  </td>";
        strTable += "\n<td><align='center' class='cssTableView' style='width:100%;' nowrap='nowrap'>  </td>";
        strTable += "\n<td><align='center' class='cssTableView' style='width:100%;' nowrap='nowrap'>  </td>";
        strTable += "\n<td><align='center' class='cssTableView' style='width:100%;' nowrap='nowrap'>  </td>";
        strTable += "\n<td><align='center' class='cssTableView' style='width:100%;' nowrap='nowrap'>PR_PRO_DEA_" + DateTime.Today.ToString("yyyyMMdd") + "</td>";
        strTable += "\n</tr>";
        strTable += "\n</table>";
        //table noi dung
        strTable += "<table cellpadding='2' cellspacing='0' class='cssTableReport'>";
        strTable += "\n<tr>";
        strTable += "\n<td style='width:12%;' class='cssTableView' nowrap='nowrap'>STT</td>";
        strTable += "\n<td style='width:12%;' class='cssTableView' nowrap='nowrap'>Công việc</td>";
        strTable += "\n<td style='width:12%;' class='cssTableView' nowrap='nowrap'>Ngày hoàn thành</td>";
        strTable += "\n<td style='width:12%;' class='cssTableView' nowrap='nowrap'>Thành viên</td>";
        strTable += "\n<td style='width:12%;' class='cssTableView' nowrap='nowrap'>Tiến độ</td>";
        strTable += "\n</tr>";
        loadDSExprort(ref strTable);
        strTable += "\n</table>";
    }

    private string loadExport()
    {
        try
        {
            string strHTML = "<html xmlns:o='urn:schemas-microsoft-com:office:office'"
            + "\n xmlns:x='urn:schemas-microsoft-com:office:excel'"
            + "\n xmlns='http://www.w3.org/TR/REC-html40'>"
            + "\n <head>"
            + "\n <meta http-equiv=Content-Type content='text/html; charset=utf-8'>"
            + "\n <meta name=ProgId content=Excel.Sheet>"
            + "\n <meta name=Generator content='Microsoft Excel 11'>"
            + "\n <link rel=File-List href='Book1_files/filelist.xml'>"
            + "\n <style id='Book1_28091_Styles'><!--table"
            + "\n 	{mso-displayed-decimal-separator:'\\.';"
            + "\n 	mso-displayed-thousand-separator:'\\,';}"
            + ".cssTitleReport"
            + "{font-family: tahoma; font-size: 11px;font-weight:normal;border: 1px #000000 solid;text-align:left;}"
            + ".cssTableView"
            + "{color:#FFFFFF;background-color:#006699;font-family: tahoma,Arial,Times New Roman; font-size: 12px;font-weight:bold;border: 1px #000000 solid;}"
            + "\n 	--></style>"
            + "\n 	</head>"
            + "\n 	<body><div id='Book1_28091' align=center x:publishsource='Excel'>";
            string strTable = "";
            loadTieuDe(ref strTable);
            strHTML += strTable;
            strHTML += "\n </div></body> ";
            strHTML += "\n </html> ";

            return strHTML;
        }
        catch
        {
            return "";
        }
    }
    #endregion

    #region Events

    

    protected void m_cbo_du_an_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {            
            load_data_2_griv();
        }
        catch (Exception v_e)
        {
            CSystemLog_301.ExceptionHandle(v_e);
        }
    }
    protected void m_txt_ngay_xem_DateChanged(object sender, EventArgs e)
    {
        try
        {
            load_data_2_griv();
        }
        catch (Exception v_e)
        {
            CSystemLog_301.ExceptionHandle(v_e);
        }
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        try
        {
            string html = loadExport();
            string strNamFile = "BaoCaoDanhSachCongViecQuaHan" + DateTime.Now.Day + "-" + DateTime.Now.Month + "-" + DateTime.Now.Year + ".xls";
            Response.Cache.SetExpires(DateTime.Now.AddSeconds(1));
            Response.Clear();
            Response.AppendHeader("content-disposition", "attachment;filename=" + strNamFile);
            Response.Charset = "UTF-8";
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.ContentType = "text/csv";
            Response.ContentType = "application/vnd.ms-excel";
            this.EnableViewState = false;
            Response.Write("\r\n");
            Response.Write(html);
            HttpContext.Current.ApplicationInstance.CompleteRequest();
        }
        catch (Exception v_e)
        {
            CSystemLog_301.ExceptionHandle(v_e);
        }
    }
    #endregion
}